home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Misc / Crossword / Source / WinDelegate.m < prev    next >
Text File  |  1992-10-11  |  4KB  |  242 lines

  1. /*
  2.  
  3. File WinDelegate.m
  4.  
  5. The window delegate file categorizes the puzzle methods that relate to the crossword window.  These methods save a puzzle to a file, as well as handling details of resizing and inspecting.
  6.  
  7. */
  8.  
  9. #import <appkit/appkit.h>
  10. #import <strings.h>
  11.  
  12. #import "WinDelegate.h"
  13. #import "Crossword.h"
  14. #import "filing.h"
  15.  
  16.  
  17. /* ————————————————————————————————————————————————————————————————————————————  */
  18.  
  19.  
  20. @implementation Puzzle (WinDelegate)
  21.  
  22.  
  23. - (BOOL) save: sender
  24. {
  25.     if ([window  isDocEdited])
  26.     if (filename[0] != '\0')
  27.             
  28.             return [self  writePuzzle: filename];    else
  29.             return [self  saveAs: self];
  30.     
  31.     return YES;
  32. }
  33.  
  34.  
  35. - (BOOL) saveAs: sender
  36. {
  37.     const char    * file;
  38.     
  39.     if ((file = fileForSave("xword")) != NULL)
  40.     if ([self  writePuzzle: file])
  41.     {
  42.         strcpy(filename, file);
  43.         [self  updateTitle];
  44.         return YES;
  45.     }
  46.     
  47.     return NO;
  48. }
  49.  
  50.  
  51. - (BOOL) saveTo: sender
  52. {
  53.     const char     * file;
  54.     
  55.     if ((file = fileForSave("xword")) != NULL)
  56.     
  57.         return [self  writePuzzle: file];        else
  58.         return NO;
  59. }
  60.  
  61.  
  62. - (BOOL) writePuzzle: (const char *) file
  63. {
  64.     NXTypedStream    * stream;
  65.     
  66.     if ((stream = NXOpenTypedStreamForFile(file, NX_WRITEONLY)) != NULL)
  67.     {
  68.         NXWriteRootObject(stream, crossword);
  69.         NXCloseTypedStream(stream);
  70.         [window  setDocEdited: NO];
  71.         return YES;
  72.     }
  73.     
  74.     else return NO;
  75. }
  76.  
  77.  
  78. /* ————————————————————————————————————————————————————————————————————————————  */
  79.  
  80.  
  81. /*
  82.  
  83. Opening a puzzle from a file is a two step process.  First the puzzle is unarchived.  Then the window is adjusted to reflect its new contents.
  84.  
  85. */
  86.  
  87.  
  88. - readPuzzle: (const char *) file;
  89. {
  90.     NXTypedStream        * stream;
  91.     
  92.     strcpy(filename, file);
  93.     stream = NXOpenTypedStreamForFile(filename, NX_READONLY);
  94.     
  95.     [self  changePuzzle: NXReadObject(stream)];
  96.     [self  new];
  97.     
  98.     NXCloseTypedStream(stream);
  99.     return self;
  100. }
  101.  
  102.  
  103. - new
  104. {
  105.     [window  makeFirstResponder: crossword];
  106.     [window  center];
  107.     [window  makeKeyAndOrderFront: self];
  108.     
  109.     return self;
  110. }
  111.  
  112.  
  113. /* ————————————————————————————————————————————————————————————————————————————  */
  114.  
  115.  
  116. - number: sender
  117. {
  118.     if ([crossword  getNumbered])
  119.             [crossword  unnumber];        else
  120.             [crossword  number];
  121.     
  122.     return self;
  123. }
  124.  
  125.  
  126. - use: sender
  127. {
  128.     [dictionary  use: sender];
  129.     return self;
  130. }
  131.  
  132.  
  133. - updateTitle
  134. {
  135.     char    * file;
  136.     char    buffer [MAXLENGTH];
  137.     int        width, height;
  138.     
  139.     [crossword  getNumRows: &height  numCols: &width];
  140.  
  141.     if (filename[0] != '\0')
  142.     {
  143.         file = rindex(filename, '/');
  144.         file[0] = '\0';
  145.         sprintf(buffer, "%s — %d x %d — %s", file + 1, height, width, filename);
  146.         file[0] = '/';
  147.     }
  148.     
  149.     else sprintf(buffer, "New Puzzle — %d x %d", height, width);
  150.     [window  setTitle: buffer];
  151.     
  152.     return self;
  153. }
  154.  
  155.  
  156. - changePuzzle: (id) newPuzzle
  157. {
  158.     NXRect        frame, old, new;
  159.     float        dw, dh;
  160.     
  161.     [newPuzzle  getBounds: &new];
  162.     [crossword  getBounds: &old];
  163.     [[window  contentView]  getBounds: &frame];
  164.     
  165.     dw = new.size.width - old.size.width;
  166.     dh = new.size.height - old.size.height;
  167.     
  168.     [window  sizeWindow: frame.size.width + dw: frame.size.height + dh];
  169.     [[window  contentView]  replaceSubview: crossword  with: newPuzzle];
  170.     [crossword  free];
  171.     crossword = newPuzzle;
  172.     
  173.     [self  updateTitle];
  174.     [crossword  setInspector: self];
  175.     
  176.     return self;
  177. }
  178.  
  179.  
  180. - update: sender
  181. {
  182.     [window  setDocEdited: YES];
  183.     return self;
  184. }
  185.  
  186.  
  187. /* ————————————————————————————————————————————————————————————————————————————  */
  188.  
  189.  
  190. /*
  191.  
  192. Here are the standard window delegate methods.  Resizing should be restricted to integral multiples of the squarewidth.
  193.  
  194. */
  195.  
  196. - windowWillResize: (id) theWindow  toSize: (NXSize *) size
  197. {
  198.     int        spaceWidth;
  199.     NXRect    frame;
  200.     
  201.     spaceWidth = [crossword  getSquareWidth] + INTERCELLWIDTH;
  202.     [theWindow  getFrame: &frame];
  203.     size->width -= ((int) (size->width - frame.size.width)) % spaceWidth;
  204.     size->height -= ((int) (size->height - frame.size.height)) % spaceWidth;
  205.     
  206.     return self;
  207. }
  208.  
  209.  
  210. - windowDidResize: sender
  211. {
  212.     [self  updateTitle];
  213.     return self;
  214. }
  215.  
  216.  
  217. - windowDidClose: sender
  218. {
  219.     [self  free];
  220.     return self;
  221. }
  222.  
  223.  
  224. - windowDidBecomeMain: sender
  225. {
  226.     id        manager;
  227.     
  228.     [manager = [FontManager  new]  setEnabled: YES];
  229.     [manager  setSelFont: [crossword  font]  isMultiple: NO];
  230.     
  231.     return self;
  232. }
  233.  
  234.  
  235. - windowDidResignMain: sender
  236. {
  237.     if ([NXApp  mainWindow] == nil) [[FontManager  new]  setEnabled: NO];
  238.     return self;
  239. }
  240.  
  241.  
  242. @end